//
//  BaseViewController.m
//  TengyunStation
//
//  Created by Johnny on 15/8/24.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "BaseViewController.h"
#import "UIScrollView+AllowPanGestureEventPass.h"
#import "TSHeader.h"
#import "NetworkViewController.h"
#import "Singleton.h"

#define NoNetworkViewTag        997

@interface BaseViewController ()<UIGestureRecognizerDelegate>

@end

@implementation BaseViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:NSStringFromClass([self class])];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:NSStringFromClass([self class])];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setIOS7LaterLayout];
    if (self.navigationController.viewControllers.count > 1) {
        if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.navigationController.interactivePopGestureRecognizer.delegate = self;
        }
        [self reloadBackButton];
    }
    self.view.backgroundColor = UIColorFromRGB(0xf1f1f1);
}

- (void)setIOS7LaterLayout {
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
}

- (void)rightBarButtonItemTitle:(NSString *)title Method:(SEL)method {
    CGSize size = [title sizeWithFont:[UIFont systemFontOfSize:15]];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, size.width, size.height);
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:OrangeColor forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    [button addTarget:self action:method forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (void)reloadBackButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 22, 22);
    [button setBackgroundColor:[UIColor clearColor]];
    [button setImage:[UIImage imageNamed:@"nav_icon_return"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backLastViewController) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (void)backLastViewController {
    [self.navigationController popViewControllerAnimated:YES];
}

//无网络界面
- (void)handelNetWorkMethod {
    _isHandleNetwork = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkMethod) name:NetworkCheckNotification object:nil];
    if (![Singleton shareInstance].isHadNetwork) {
        [self showNoNetworkView];
    }
}

- (void)checkNetworkMethod {
    if ([Singleton shareInstance].isHadNetwork) {
        [self hideNoNetWorkView];
    } else {
        [self showNoNetworkView];
    }
}

- (void)showNoNetworkView {
    if (!_noNetworkView) {
        _noNetworkView = [UIButton buttonWithType:UIButtonTypeCustom];
        _noNetworkView.frame = CGRectMake(0, 0, 320, 44);
        _noNetworkView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
        _noNetworkView.tag = NoNetworkViewTag;
        [_noNetworkView addTarget:self action:@selector(networkButtonMethod) forControlEvents:UIControlEventTouchUpInside];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 11, 22, 22)];
        imageView.image = [UIImage imageNamed:@"network_logo"];
        [_noNetworkView addSubview:imageView];
        UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(44, 12, 200, 20)];
        tipLabel.backgroundColor = [UIColor clearColor];
        tipLabel.font = [UIFont systemFontOfSize:13];
        tipLabel.textColor = [UIColor whiteColor];
        tipLabel.text = @"网络请求失败，请检查您的网络！";
        [_noNetworkView addSubview:tipLabel];
        UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(290, 11, 22, 22)];
        imageView1.image = [UIImage imageNamed:@"netwrok_next"];
        [_noNetworkView addSubview:imageView1];
    }
    if (![self.view viewWithTag:NoNetworkViewTag]) {
        [self.view addSubview:_noNetworkView];
    }
}

- (void)hideNoNetWorkView {
    if ([self.view viewWithTag:NoNetworkViewTag]) {
        [_noNetworkView removeFromSuperview];
    }
}

- (void)networkButtonMethod {
    NetworkViewController *networkVC = [[NetworkViewController alloc] initWithNibName:@"NetworkViewController" bundle:nil];
    [self.navigationController pushViewController:networkVC animated:YES];
}

- (void)dealloc {
    if (_isHandleNetwork) {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:NetworkCheckNotification object:nil];
    }
}

@end
